package im.yisme.view.ui.util;

import im.yisme.R;
import im.yisme.view.activities.LyricsLibraryActivity;

import java.util.ArrayList;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class LyricsLibAdapter extends BaseAdapter{
	private ArrayList<Map<String, Object>> data1, data2;
	private Context context;
	private LyricsLibraryActivity activity;
	
	public LyricsLibAdapter(Context context, 
							LyricsLibraryActivity activity,
							ArrayList<Map<String, Object>> data1,
							ArrayList<Map<String, Object>> data2) {
			this.context = context;
			this.activity = activity;
			this.data1 = data1;
			this.data2 = data2;
	}
	
	public int getCount() {
		// TODO Auto-generated method stub
		return data1.size()+data2.size()+2;
	}

	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return arg0;
	}

	public View getView(final int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		if(position == 0){
			
			convertView = LayoutInflater.from(context).inflate(
					R.layout.list_divider_original, null);
			
		}
		else if(position<data1.size()+1){
			TextView lyricsName = null;
			Button deleteButton = null;
			
				convertView = LayoutInflater.from(context).inflate(
						R.layout.lyricslib_listitem_original, null);	
			
			
			
			lyricsName = (TextView) convertView.findViewById(R.id.textView_name);			
			deleteButton = (Button) convertView.findViewById(R.id.button_delete);
			deleteButton.setOnClickListener(new OnClickListener() {
				
				public void onClick(View v) {
					// TODO Auto-generated method stub
					int position1 = position - 1;
					activity.deleteLyrics(position1, 1);
				}
			});
			
			Map<String, Object> map=data1.get(position-1);
			
			lyricsName.setText((String)map.get("lyricsName"));
			
		}
		else if(position == data1.size()+1){
						
				convertView = LayoutInflater.from(context).inflate(
						R.layout.list_divider_nonoriginal, null);
			
		}
		else {
			
			TextView lyricsName = null;
            Button deleteButton = null;
			ImageView official = null;
			
					
				convertView = LayoutInflater.from(context).inflate(
					R.layout.lyricslib_listitem_nonoriginal, null);				
			
			
			lyricsName = (TextView) convertView.findViewById(R.id.textView_name);
			deleteButton = (Button) convertView.findViewById(R.id.button_delete);
			official = (ImageView) convertView.findViewById(R.id.imageView_official);
			deleteButton.setOnClickListener(new OnClickListener() {
				
				public void onClick(View v) {
					// TODO Auto-generated method stub
					int position1 = position - data1.size() - 2;
					activity.deleteLyrics(position1, 2);
				}
			});
			
			Map<String, Object> map=data2.get(position-data1.size()-2);
			lyricsName.setText((String)map.get("lyricsName"));
			
			boolean isOfficial=(Boolean) map.get("official");
			if(!isOfficial) official.setVisibility(View.INVISIBLE);
		}
		
		
		
		return convertView;
	}

}
